﻿Ext.define('app.prm.detail.EditInfo',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    //maximized: true,
    width: 850,
    height: 600,
    layout: 'fit',
    projectId: 0,
    CustomerId: '',
    ProductId: '',
    initComponent: function () {
        var me = this;
        me.items = [{
            xtype: 'container',
            anchor: '100%',
            autoScroll: true,
            layout: 'hbox',
            items: [
                {
                    xtype: 'container',
                    flex: 2,
                    layout: 'anchor',
                    items: [me.getForm()]
                },
                {
                    xtype: 'container',
                    flex: 1,
                    bodyStyle: 'padding:5px',
                    items: [me.getPhaseTree()]
                }
            ]
        }];
        me.buttons = [
            { text: localize('Save'), scope: this, handler: me.onSubmit },
            { text: localize('Close'), handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxGet('/api/services/app/project/Get?id=' + me.projectId, function (data) {
                var cbo_tree = me.getComboTree();
                var cboValue = "";
                if (data.result.businessId > 0) {
                    cboValue = data.result.business;
                }
                if (data.result.rdCenterId > 0) {
                    cboValue = data.result.rdCenter;
                }
                if (data.result.laboratoryId > 0) {
                    cboValue = data.result.laboratory;
                }


                cbo_tree.setRawValue(cboValue);
                cbo_tree.BusinessId = data.result.businessId;
                cbo_tree.RdCenterId = data.result.rdCenterId;
                cbo_tree.LaboratoryId = data.result.laboratoryId; 
                var cbo_principal = me.form.query("combo[name=PMId]")[0];
                cbo_principal.setValue(data.result.pmId);
                cbo_principal.setRawValue(data.result.pm);
                me.form.getForm().setValues(data.result);

                var combCustomer = me.form.query("combo[name=customer]")[0];
                //combCustomer.setValue(1);
                me.CustomerId = data.result.customer;
                combCustomer.setRawValue(data.result.customer);



                var productComber = me.form.query("combo[name=productNo]")[0];
                me.ProductId = data.result.productNo;
                productComber.setRawValue(data.result.productNo);

                me.getRadioGroup().setValue({ ReportWeek: data.result.reportWeek });
            });
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                defaults: { anchor: '100%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        hidden: true,
                        name: 'id',
                        value: 0
                    },
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'displayfield',
                                        fieldLabel: localize('ProjectNo'),
                                        name: 'projectNo'
                                    }, {
                                        xtype: 'combo',
                                        reference: 'CustomerCombo',
                                        name: 'customer',
                                        allowBlank: false,
                                        fieldLabel: '客户名称',
                                        emptyText: '输入姓名查询',
                                        store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetCustomers', autoLoad: false }),
                                        queryParam: 'name',
                                        typeAhead: false,
                                        minChars: 1,
                                        queryMode: 'remote',
                                        triggerAction: 'query',
                                        displayField: 'name',
                                        valueField: 'id'
                                    },

                                    {
                                        xtype: 'combo',
                                        name: 'productNo',
                                        allowBlank: false,
                                        fieldLabel: '产品',
                                        emptyText: '输入产品查询',
                                        store: Ext.create("app.ux.PagePrmStore", { url: '/api/services/app/product/GetAll', autoLoad: false }),
                                        queryParam: 'code',
                                        typeAhead: false,
                                        minChars: 1,
                                        queryMode: 'remote',
                                        triggerAction: 'query',
                                        displayField: 'code',
                                        valueField: 'id'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: localize('QualityStandard'),
                                        name: 'qualityStandard'
                                    },
                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        fieldLabel: localize('TargetEndDate'),
                                        name: 'targetEndDate'
                                    },
                                    {
                                        xtype: 'combo',
                                        //id: 'cbo_principal',
                                        name: 'PMId',
                                        hidden: true,
                                        fieldLabel: '项目经理',
                                        emptyText: '输入姓名查询',
                                        store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetUsers', autoLoad: false }),
                                        queryParam: 'name',
                                        typeAhead: false,
                                        minChars: 1,
                                        queryMode: 'remote',
                                        triggerAction: 'query',
                                        displayField: 'name',
                                        valueField: 'id'
                                    },
                                     me.getComboTree()
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: localize('ProjectName'),
                                        name: 'projectName'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: localize('ProjectNature'),
                                        name: 'projectNature'
                                    },
                                    {
                                        xtype: 'combo',
                                        editable: false,
                                        hidden: true,
                                        fieldLabel: localize('TaskType'),
                                        name: 'taskType',
                                        store: ['小试', '中试', '其它']
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: localize('PilotLocation'),
                                        name: 'address'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: localize('TargetWorkCost'),
                                        name: 'targetWorkCost'
                                    },
                                    {
                                        xtype: 'textfield', hidden: true,
                                        fieldLabel: localize('TargetCost'),
                                        name: 'targetCost'
                                    },
                                    {
                                        xtype: 'textfield', hidden: true,
                                        fieldLabel: localize('TargetMaterialCost'),
                                        name: 'targetMaterialCost'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        fieldLabel: localize('TargetOutPut'),
                                        name: 'targetOutPut'
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: localize('CustomerCode'),
                                        name: 'customeCode'
                                    },


                                  me.getRadioGroup(),

                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        fieldLabel: localize('PlanStartDate'),
                                        name: 'planStartDate'
                                    },
                                    {
                                        xtype: 'datefield',
                                        editable: false,
                                        format: 'Y-m-d',
                                        allowBlank: false,
                                        fieldLabel: localize('PlanEndDate'),
                                        name: 'planEndDate'
                                    }
                                ]
                            }
                        ]
                    },
                    //{
                    //    xtype: 'tagfield',
                    //    anchor: '85%',
                    //    //id: 'txt_Phase',
                    //    fieldLabel: '项目阶段',
                    //    name: 'phaseIds',
                    //    allowBlank: false,
                    //    store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/phaseInfo/GetPhaseInfo' }),
                    //    valueField: 'id',
                    //    displayField: 'name'
                    //},
                    {
                        xtype: 'htmleditor',
                        fieldLabel: localize('Remark'),
                        height: 220,
                        anchor: '85%',
                        name: 'remark'
                    }
                ]
            });
        }
        return this.form;
    },
    getRadioGroup: function () {
        if (!this.radiogroup) {
            this.radiogroup = new Ext.form.RadioGroup(
            {
                fieldLabel: '客户周报',
                // columns: 5,
                vertical: true,
                items: [
                    { boxLabel: '单周报', name: 'ReportWeek', inputValue: '1', checked: true },
                    { boxLabel: '双周报', name: 'ReportWeek', inputValue: '2' }
                ]
            });
        }
        return this.radiogroup;
    },
    getPhaseTree: function () {
        var me = this;
        if (!this.phaseTree) {
            this.phaseTree = new Ext.tree.Panel({
                border: false,
                useArrows: true,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/project/GetProjectPhaseCheckTree?projectId=' + me.projectId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    },
                    cellclick: function (table, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                        if (cellIndex == 0) {
                            record.set('checked', !record.data.checked);;
                            if (record.data.checked) {
                                record.eachChild(function (child) {
                                    me.checkChild(child, true);
                                });
                            }
                            else {
                                record.eachChild(function (child) {
                                    me.checkChild(child, false);
                                });
                            }
                            me.checkParent(record);// 进行父级选中操作
                        }
                    }
                }
            });
        }
        return this.phaseTree;
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, check);
            });
        }
    },
    nodep: function (node) {
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {
            if (me.nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            me.checkParent(node.parentNode);
        }
    },
    getComboTree: function () {
        if (!this.comboTree) {
            this.comboTree = Ext.create("app.ux.ComboTreeBox", {
                //id: 'cbo_tree',
                //reference: 'cbo_tree',
                fieldLabel: '负责科室',
                editable: false,
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true
                    },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/laboratory/GetTree'
                    }
                })
            });
        }
        return this.comboTree;
    },
    onSubmit: function () {
        var me = this;
        var arry = me.getPhaseTree().getChecked();
        var phases = [];
        for (var i = 0; i < arry.length; i++) {
            var node = arry[i];
            if (node.data.id != "0") {
                phases.push(parseInt(node.data.id));
            }
        }
        var cbo_tree = me.getComboTree();
        var json = me.getForm().getForm().getValues();
        json["BusinessId"] = cbo_tree.BusinessId;
        json["RdCenterId"] = cbo_tree.RdCenterId;
        json["LaboratoryId"] = cbo_tree.LaboratoryId;
        json["ReportWeek"] = me.getRadioGroup().getChecked()[0].inputValue;
        json["PhaseIds"] = phases;
        var comboCustomer = me.form.query("combo[name=customer]")[0];
        json["CustomerId"] = comboCustomer.getValue();
        if (json["CustomerId"] != me.CustomerId && (json["CustomerId"] == '' || isNaN(json["CustomerId"]))) {
            Ext.Msg.alert('系统提示', "请通过选择录入客户");
            return false;
        }
        json["customer"] = comboCustomer.getRawValue();



        var comboProduct = me.form.query("combo[name=productNo]")[0];
        json["ProuductId"] = comboProduct.getValue();
        if (json["ProuductId"] != me.ProductId && (json["ProuductId"] == '' || isNaN(json["ProuductId"]))) {
            Ext.Msg.alert('系统提示', "请通过选择录入产品号");
            return false;
        }
        json["productNo"] = comboProduct.getRawValue();

        App.AjaxPost('/api/services/app/project/EditProject', json, function () {
            me.fireEvent("savesuccess", json);
        }, function () { Ext.Msg.alert('系统提示', '请重新选择负责科室'); });
    }
});